/*
  	Arkodroid - An Arduino Controller for Android
	Copyright (c) 2013 Miguel Monteiro, Eduardo Peixoto, Rui Pedro Carvalhal, André Braga.
	All rights reserved.
	http://code.google.com/p/arkodroid/
	
	Amarino API by Bonifaz Kaufmann - http://www.amarino-toolkit.net/
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package net.miguelmonteiro.android.arkodroid;

import net.miguelmonteiro.android.arkodroid.Checker.Resource;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ToggleButton;
import at.abraxas.amarino.Amarino;

public class ArkodroidJoysticks extends Activity {    
	// Variables
	long lastchanged;

	public static final String TAG = "ArkodroidJoysticks";
	
	// Declare objects
	TextView xCoor;
	TextView yCoor;
    
    Button buzzerButton;
    ToggleButton toggleButton;

    JoystickView joystickL;
    JoystickView joystickR;
	
    /** Check if Bluetooth is ON **/
	@Override 
    protected void onResume() {
		super.onResume();
		
    	new Checker(this).pass(new Checker.Pass() {
    		@Override public void pass() {
    	    	// Nothing
    	 	}
    	}).check(Resource.BLUETOOTH);
    }

    /** Called when the activity is first created. **/
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main_joysticks);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        lastchanged = System.currentTimeMillis();
        
        // Create objects
        xCoor = (TextView)findViewById(R.id.xcoor);
		yCoor = (TextView)findViewById(R.id.ycoor);
		buzzerButton = (Button) findViewById(R.id.buttonBuzzer);

        joystickL = (JoystickView)findViewById(R.id.joystickViewL);
        joystickR = (JoystickView)findViewById(R.id.joystickViewR);

        joystickL.setOnJostickMovedListener(_listenerL);
        joystickR.setOnJostickMovedListener(_listenerR);

		
		buzzerButton.setOnClickListener(new View.OnClickListener() {
	        public void onClick(View v) {
	        	Log.i(TAG, "Buzzer command sent");
	        	Amarino.sendDataToArduino(ArkodroidJoysticks.this, BluetoothConnect.DEVICE_ADDRESS, 'b', (1));
	        }
	    });
    }
	
	public void onToggleClicked(View v) {
		Arkodroid.doLightToggleClick(this, v);
    }
	
	private JoystickMovedListener _listenerL = new JoystickMovedListener() {
		@Override
        public void OnMoved(int pan, int tilt) {
			yCoor.setText("Y: " + Integer.toString(tilt));
			Arkodroid.values[0] = Integer.toString(tilt);
			Arkodroid.sendValues(ArkodroidJoysticks.this);
		}

        @Override
        public void OnReleased() {
        	yCoor.setText("Y: released");
        	Arkodroid.values[0] = Integer.toString(0);
			Arkodroid.sendStopValues(ArkodroidJoysticks.this);
        }
	}; 
	
	private JoystickMovedListener _listenerR = new JoystickMovedListener() {
		@Override
        public void OnMoved(int pan, int tilt) {
			xCoor.setText("X: " + Integer.toString(pan));
			Arkodroid.values[1] = Integer.toString(pan);
			Arkodroid.sendValues(ArkodroidJoysticks.this);
		}

        @Override
        public void OnReleased() {
        	xCoor.setText("X: released");
        	Arkodroid.values[1] = Integer.toString(0);
			Arkodroid.sendStopValues(ArkodroidJoysticks.this);
        }
	};
	
	/** Hard "Back button" pressed **/
	@Override
    public void onBackPressed() {
		Arkodroid.exit(this);
    } 
    
	/** Menu creation **/
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.layout.menu, menu);        
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        	case R.id.connecttext:
            	startActivity(new Intent(this, BluetoothConnect.class));                
            	return true;
        	case R.id.musictext:
        		Arkodroid.selectMusic(this);
        		return true;
            case R.id.settingstext:
            	finish();
            	startActivity(new Intent(this, net.miguelmonteiro.android.arkodroid.Settings.class));
                return true;
            case R.id.abouttext:
                Arkodroid.aboutDialog(this);
                return true;
        }
        return true;
    }
}
